package com.bcnapps.DemoGame;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Protagonista {
	
	Paint paint;
	Bitmap prota;
	Objecte o;
	boolean intheair;
	
	public Protagonista(Resources res, float speed) {
		paint = new Paint();
		paint.setColor(0xFF000000);
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		prota = BitmapFactory.decodeResource(res, R.drawable.koopaspriteforward,options);
		//o = new Objecte(prota,100,GV.Screen.metrics.heightPixels/2,GV.heightpercent(0.10f),GV.heightpercent(0.10f),speed,0);
		o = new Objecte(prota,100,GV.Screen.metrics.heightPixels/2,GV.heightpercent(0.15f),GV.heightpercent(0.15f),speed,0,4,2,3);
		intheair = true;
	}
	
	public void actualitza(Plataformes plat) {
		o.vy += (int)GV.heightpercent(0.01f);	//efecte gravetat, ha de ser un enter!
		if(o.vy<=0) {
			o.actualitza();
		}
		else {
			for(int i = 0; i<=(int)(o.vy)+1; i++) {
				if(plat.esta_sobre(o.x,o.tx,o.y+i,o.ty)) {
					o.y += i;
					o.vy=0;
				}
			}
			o.actualitza();
		}
		//salts
		intheair = !plat.esta_sobre(o.x,o.tx,o.y,o.ty);
		//et morts
		if(o.y>GV.Screen.metrics.heightPixels) {
			o.x = 100;
			o.y = GV.Screen.metrics.heightPixels/2;
		}
		//sprites
		if(o.acabat) {
			o.sfcount = 0;
			o.acabat = false;
		}
	}
	
	public void draw(Canvas canvas) {
		float ox = o.x;
		o.x = 100;
		o.draw(canvas);
		o.x = ox;
		//canvas.drawBitmap(o.img,100, o.y, null);
	}

	public void salta() {
		if(intheair) return;
		o.vy -= (int)GV.heightpercent(0.065f);
		intheair = true;
	}
	
	public float getX() {
		return o.x;
	}
}
